package com.aletto.general_store.ui.activities.orders;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.aletto.general_store.R;
import com.aletto.general_store.data.DataRepository;
import com.aletto.general_store.data.model.OrderDetails;
import com.aletto.general_store.data.model.OrderItem;
import com.aletto.general_store.presenters.OrderRefundTask;
import com.aletto.general_store.presenters.core.SimpleTemplate;
import com.aletto.general_store.utils.XHelper;
import com.aletto.general_store.utils.image.GlideApp;
import com.sisyphus.spinach.core.INetTask;
import com.sisyphus.spinach.network.presenters.NetSingleTask;

import java.math.BigDecimal;
import java.util.List;

// 个人店铺，退款
public class OrderStoreAfterSaleActivity extends AppCompatActivity {

    public static Intent newIntent(Context context, OrderItem item) {
        Intent intent = new Intent(context, OrderStoreAfterSaleActivity.class);
        intent.putExtra("ITEM", item);
        return intent;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_store_after_sale);

        OrderItem item = (OrderItem) getIntent().getSerializableExtra("ITEM");

        XHelper.initTitle(this, R.string.order_delivery_details);

        // 地址
        findViewById(R.id.layout_address).setVisibility(item.getGoods_type() == 1 ? View.GONE : View.VISIBLE);

        // 价格
        BigDecimal price = new BigDecimal(item.getPrice()).multiply(new BigDecimal(item.getNum()));
        TextView tv_goods_price = findViewById(R.id.tv_goods_price);
        tv_goods_price.setText("买家已付：" + price + " USDT");

        // 数量
        TextView tv_sold_number = findViewById(R.id.tv_sold_number);
        tv_sold_number.setText("× " + item.getNum());

        // 商品图片
        List<String> goodsImg = item.getGoods_img();
        if (goodsImg != null && !goodsImg.isEmpty()) {
            ImageView iv_goods_cover = findViewById(R.id.iv_goods_cover);
            GlideApp.with(iv_goods_cover).load(goodsImg.get(0)).normal().into(iv_goods_cover);
        }

        // 商品名称
        TextView tv_goods_title = findViewById(R.id.tv_goods_title);
        tv_goods_title.setText(item.getGoods_name());

        if (item.getStatus() == 5 || item.getStatus() == 6) { // 买家申请退款
            findViewById(R.id.tv_confirm).setOnClickListener(v -> // 同意退款
                    new OrderRefundTask(item.getBtc_order_id()).execute());

        } else {

            findViewById(R.id.tv_store_customer).setVisibility(View.INVISIBLE);
            ((TextView) findViewById(R.id.tv_store_delivery)).setText(R.string.order_service);
            ((TextView) findViewById(R.id.tv_logistics)).setText(R.string.order_store_customer);
            ((TextView) findViewById(R.id.tv_confirm)).setText(R.string.order_logistics);
        }

        // 执行任务
        task(item.getOrder_id()).execute(getLifecycle());
    }

    private SimpleTemplate<OrderDetails> task(String id) {
        return new SimpleTemplate<>() {
            @Override
            public INetTask.View<OrderDetails> view() {
                return new INetTask.View<>() {
                    @Override
                    public void success(OrderDetails data) {
                        OrderDetails.AddressDTO addressDTO = data.getAddress();

                        // 联系地址
                        TextView tv_address = findViewById(R.id.tv_address);
                        tv_address.setText(addressDTO.getAddress());

                        // 联系人
                        TextView tv_mobile_number = findViewById(R.id.tv_mobile_number);
                        tv_mobile_number.setText(addressDTO.getName() + "\t\t\t" + addressDTO.getPhone());

                        // 订单编号
                        TextView tv_order_number = findViewById(R.id.tv_order_number);
                        tv_order_number.setText(data.getOrder_id());
                        XHelper.copy2Clipboard(tv_order_number.getText());

                        // 订单时间
                        TextView tv_pay_time = findViewById(R.id.tv_pay_time);
                        tv_pay_time.setText(data.getPay_time());

                        // 支付网络
                        TextView tv_pay_channel = findViewById(R.id.tv_pay_channel);
                        tv_pay_channel.setText(data.getPay_network());

                        // 支付哈希
                        TextView tv_pay_hash = findViewById(R.id.tv_pay_hash);
                        tv_pay_hash.setText(data.getPay_hash());
                        XHelper.copy2Clipboard(tv_pay_hash.getText());
                    }
                };
            }

            @Override
            public INetTask.Presenter<OrderDetails, INetTask.View<OrderDetails>> presenter() {
                return new NetSingleTask<>(DataRepository.getInstance().orderDetails(id));
            }
        };
    }

}
